/*
 * bspADC.c
 *
 *  Created on: 2024��9��5��
 *      Author: zizhong
 */
#include "bspADC.h"
#include "Uart.h"
#include "dac.h"
#include <stdlib.h>
#include "ipc.h"
#include "bspIpc.h"
#include "foc_declare.h"
#include "motor_para.h"
#include "measure.h"

extern void buildLevel01(void);
extern void buildLevel02(void);
extern void buildLevel03(void);

extern MOTOR_Vars_t motorVars;

#define  one_by_4096   0.000244


uint32_t adcIntCnt = 0;

float clarke1AlphaDlog = 0.0;
float clarke1BetaDlog = 0.0;
float UalphaDlog = 0.0;
float UbetaDlog = 0.0;

float Va, Vb, Vc, Ia, Ib, Ic, PVDD;
extern uint32_t rxCnt;

float readData[7];
#pragma DATA_SECTION(readData, "MSGRAM_CPU1_TO_CPU2")

extern IPC_MessageQueue_t messageQueue;
extern IPC_Message_t      TxMsg;

extern MOTOR_Vars_t motorVars;

__interrupt void INT_myADC0_1_ISR(void)
{
    // IC/PVDD
    // IB/VC
    // IA/VB/VA
    GPIO_writePin(131, 1);


//    buildLevel01();



    GPIO_writePin(131, 0);


//    Ia = (float)AdccResultRegs.ADCRESULT0 * one_by_4096 * 3.0;
//    Ib = (float)AdcbResultRegs.ADCRESULT0 * one_by_4096 * 3.0;
//    Ic = (float)AdcaResultRegs.ADCRESULT0 * one_by_4096 * 3.0;
//    Va = (float)AdccResultRegs.ADCRESULT2 * one_by_4096 * 3.0 * 13.333;
//    Vb = (float)AdccResultRegs.ADCRESULT1 * one_by_4096 * 3.0 * 13.333;
//    Vc = (float)AdcbResultRegs.ADCRESULT1 * one_by_4096 * 3.0 * 13.333;
//    PVDD = (float)AdcaResultRegs.ADCRESULT1 * one_by_4096 * 3.0 * 13.333;


    readData[0] = 0.0;
    readData[1] = 1.1;
    readData[2] = 2.2;
    readData[3] = 3.3;
    readData[4] = 4.4;
    readData[5] = 5.5;


    //
    // Update the message
    //
    TxMsg.command = IPC_CMD_READ_MEM;
    TxMsg.address = (uint32_t)readData;
    TxMsg.dataw1  = 6;  // Using dataw1 as data length
    TxMsg.dataw2  = 1;   // Message identifier



    IPC_sendMessageToQueue(IPC_CPU1_L_CPU2_R, &messageQueue, IPC_ADDR_CORRECTION_ENABLE,
                           &TxMsg, IPC_BLOCKING_CALL);


    ADC_clearInterruptStatus(myADC0_BASE, ADC_INT_NUMBER1);
    Interrupt_clearACKGroup(INT_myADC0_1_INTERRUPT_ACK_GROUP);

}

//DAC TIPS:
//    uint16_t dacDebugValue0 = (uint16_t)((sin + 1.0F) * 0.5F * 4096.0F);
//    uint16_t dacDebugValue1 = (uint16_t)((puAngle + 1.0F) * 0.5F * 4096.0F);
//    DAC_setShadowValue(myDAC0_BASE, dacDebugValue0);
//    DAC_setShadowValue(myDAC1_BASE, dacDebugValue1);
